﻿@{
    ViewBag.Title = "Auto Fetch";
}

<h2>Auto Fetch</h2>
<div id="loadResult"></div>
@section Scripts{
    <script>
     
        $(function () {                 
            //getFetchList();
        })
        function getFetchList() {
            $.ajax({
                type: "POST",
                url: '@Url.Action("FetchList", "AutoFetch")',
                dataType: "json",
                success: function (data) {
                    console.log('getFetchList', data);                    
                    if(data.state=="success")
                    {
                        $.each(data.list, function (index, item) {                          
                            setTimeout(function () {
                                console.log(index, item.Url, item.Id);
                                loadUrl(item.Url);
                            }, 3000 * index);
                        })
            
                    }
                }
            });
        }

        function loadUrl(url)
        {
            $("#loadResult").load("../AutoFetch/Other?url=" + url + " #contentHtml", {}, function (response, status, xhr) {
                if (status == "success") {
                    $("#loadResult .listw li a").each(function (index, item) {
                        var txt = $(item).text();
                        var detailUrl = "http://chengyu.t086.com" + $(item).attr("href");
                        var msg = txt + "==>" + detailUrl;
                        writeLog(url, txt, detailUrl);
                    })
                }
            })
        }

        function writeLog(url, txt, detailUrl) {
            $.ajax({
                type: "POST",
                url: '@Url.Action("WriteLog", "AutoFetch")',
                dataType: "json",
                data: {
                    url: url,
                    txt: txt,
                    detailUrl: detailUrl
                },
                success: function (data) {
                    //console.log('writeLog', data);
                }
            });
        }
    </script>
}

